package com.vmap.longitude;

import java.util.List;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class LongitudeActivity extends MapActivity {
	 
 MapController mapController;
 MyPositionOverlay positionOverlay; 	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	 super.onCreate(savedInstanceState);
	 setContentView(R.layout.main);
	
 	   // Get a reference to the MapView
	 MapView myMapView = (MapView)findViewById(R.id.myMapView);
	 // Get the Map View’s controller
	mapController = myMapView.getController();
	// Configure the map display options
	myMapView.setSatellite(false);
	myMapView.setStreetView(true);
	myMapView.displayZoomControls(true);
	// Zoom in
	mapController.setZoom(17);  
 
	// Add the MyPositionOverlay
	positionOverlay = new MyPositionOverlay();
	List<Overlay> overlays = myMapView.getOverlays();
	overlays.add(positionOverlay);

	LocationManager locationManager;
	String context = Context.LOCATION_SERVICE;
	locationManager = (LocationManager)getSystemService(context);
		
	    //Define Criteria object
	    Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setCostAllowed(true);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		
	 //String provider = LocationManager.NETWORK_PROVIDER; 	
	String provider = locationManager.getBestProvider(criteria, true);
	Location location =locationManager.getLastKnownLocation(provider);
	
	//in case location is still null , use values from Network Provider
	if (location ==null)
	{ 
        
		Log.d("androLongitude","location was null , using networkprovider");
		location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
	}
	updateWithNewLocation(location);
	locationManager.requestLocationUpdates(provider, 2000, 10,locationListener);
	}
	
	
	@Override
	 protected boolean isRouteDisplayed() {
		return false; 
	}
	
    
    private final LocationListener locationListener = new LocationListener() {
    	public void onLocationChanged(Location location) {
    	updateWithNewLocation(location);
    	}
    	public void onProviderDisabled(String provider){
    	updateWithNewLocation(null);
    	}
    	public void onProviderEnabled(String provider){ }
    	public void onStatusChanged(String provider, int status,
    	Bundle extras){ }
    	};
   
   @Override
  public boolean onCreateOptionsMenu(Menu menu) {
  MenuInflater inflater = getMenuInflater();
   inflater.inflate(R.menu.menu, menu);
   return true;
    }
   
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
       // Handle item selection
       switch (item.getItemId()) {
           case R.id.track_time:
        	   Toast.makeText(this, "Dummy toast, give a call to your activity", Toast.LENGTH_LONG).show(); 
               return true;
           case R.id.help:
        	   Toast.makeText(this, "Dummy toast, give a call to your activity", Toast.LENGTH_LONG).show();
               return true;
           default:
               return super.onOptionsItemSelected(item);
       }
   } 

    	
    public void updateWithNewLocation(Location location) { 
    	
     	String latLongString;
    		
    	String addressString = "No address found";
    	 if (location != null) {
    		  //update the overlay
    		 positionOverlay.setLocation(location);
    		 
    		// Update the map location.
    		 Double geoLat = location.getLatitude()*1E6;
    		 Double geoLng = location.getLongitude()*1E6;
    		 GeoPoint point = new GeoPoint(geoLat.intValue(),
    		 geoLng.intValue());
    	     mapController.animateTo(point);
    		  
    	      Log.d("androLongitude","location.getLatitude returned " + geoLat); 
    	      Log.d("androLongitude","location.getLongitude returned " + geoLng); 
    	     
/*    		 double lat = location.getLatitude();
    		 double lng = location.getLongitude();
    		 latLongString = "Lat:" + lat + "\nLong:" + lng;

    		Geocoder gc = new Geocoder(this, Locale.getDefault());
    		try {
    		List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
    		StringBuilder sb = new StringBuilder();
    		if (addresses.size() > 0) {
    		Address address = addresses.get(0);
    		for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
    		sb.append(address.getAddressLine(i)).append("\n");
    		sb.append(address.getLocality()).append("\n");
    		sb.append(address.getPostalCode()).append("\n");
    		sb.append(address.getCountryName());
    		}
    		addressString = sb.toString();
    		} catch (IOException e) {}
    		} else {
    		latLongString = "No location found";
    		}
 
    	*/
      
    	 }
    	 }    
     
}